/**
 * 弹窗-自定义内容
 * @param {String} selector id或class等选择器，目前仅id
 */
function showDialog(selector) {
  var dialog = document.createElement('div')
  var dialogContent = document.createElement('div')
  var content = ''

  dialog.setAttribute('class', 'm-dialog')
  dialog.style = 'position:fixed;top:0;left:0;width:100%;height:100%;z-index:1200;background:rgba(0,0,0,.4)'

  dialogContent.style = 'position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);z-index:999;width:86%;max-width:640px;max-height:88%;overflow-y:auto;'

  if (selector.indexOf('#') === 0) {
    content = document.getElementById(selector.substring(1)).cloneNode(true)
    content.style.display = 'block'
    content.setAttribute('data-modelid', content.id)
    content.id = ''
  }
  dialogContent.append(content)
  dialog.append(dialogContent)

  document.body.append(dialog)
}

function closeDialog() {
  var dialogs = document.querySelectorAll('.m-dialog')
  var theDialog = dialogs[dialogs.length - 1]
  var dialogMain = theDialog.querySelector('.dialog-main')
  if (dialogMain && dialogMain.dataset.modelid) {
    // 同步修改到弹窗模板
    var newModel = dialogMain.cloneNode(true)
    var pageModel = document.getElementById(dialogMain.dataset.modelid)
    if (pageModel) {
      newModel.style.display = 'none'
      newModel.id = dialogMain.dataset.modelid
      pageModel.parentElement.replaceChild(newModel, pageModel)
    }
  }
  document.body.removeChild(theDialog)
}
